#!/usr/bin/env python
"""
微信聊天助手测试执行脚本
"""
import os
import sys
import argparse
import subprocess


def run_tests(test_type=None, verbose=False, coverage=False):
    """执行测试"""
    # 构建命令
    cmd = ["pytest"]
    
    # 添加详细输出
    if verbose:
        cmd.append("-v")
    
    # 添加测试覆盖率
    if coverage:
        cmd.extend(["--cov=app", "--cov-report=term", "--cov-report=html"])
    
    # 根据测试类型选择测试目录
    if test_type == "unit":
        cmd.extend(["tests/test_models", "tests/test_crud"])
    elif test_type == "integration":
        cmd.append("tests/test_integration")
    elif test_type == "api":
        cmd.append("tests/test_api")
    elif test_type == "performance":
        cmd.append("tests/performance")
    else:
        # 执行所有测试(除了性能测试)
        cmd.extend(["tests/test_models", "tests/test_crud", "tests/test_integration", "tests/test_api"])
    
    # 执行命令
    print(f"执行命令: {' '.join(cmd)}")
    return subprocess.call(cmd)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="微信聊天助手测试执行脚本")
    parser.add_argument(
        "--type", 
        type=str, 
        choices=["unit", "integration", "api", "performance", "all"],
        default="all",
        help="测试类型: unit(单元测试), integration(集成测试), api(API测试), performance(性能测试), all(除性能测试外的所有测试)"
    )
    parser.add_argument(
        "--verbose", "-v", 
        action="store_true", 
        help="输出详细日志"
    )
    parser.add_argument(
        "--coverage", 
        action="store_true", 
        help="生成测试覆盖率报告"
    )
    
    args = parser.parse_args()
    
    # 确保工作目录正确
    script_dir = os.path.dirname(os.path.abspath(__file__))
    os.chdir(script_dir)
    
    # 运行测试
    sys.exit(run_tests(
        test_type=args.type if args.type != "all" else None,
        verbose=args.verbose,
        coverage=args.coverage
    )) 